﻿using System;
using System.Text.RegularExpressions;

namespace Backend.JYD.Common
{
    /// <summary>
    /// 数字处理类
    /// </summary>
    public class NumberHelper
    {

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression)
        {
            return ObjectToInt(expression, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object expression, int defValue)
        {
            if (expression != null && !(expression is System.DBNull))
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str)
        {
            return StrToInt(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !IsInt(str.Trim()))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(str, defValue));
        }

        /// <summary>
        /// 将对象转换为Int64类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static Int64 ObjectToInt64(object expression, Int64 defValue)
        {
            if (expression != null && !(expression is System.DBNull))
                return StrToInt64(expression.ToString(), defValue);

            return defValue;
        }

        public static Int64 ObjectToInt64(object expression)
        {
            Int64 defValue = 0;
            if (expression != null && !(expression is System.DBNull))
                return StrToInt64(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int64类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static Int64 StrToInt64(string str)
        {
            return StrToInt64(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int64类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static Int64 StrToInt64(string str, Int64 defValue)
        {
            if (string.IsNullOrEmpty(str))
                return defValue;

            long rv;
            if (Int64.TryParse(str, out rv))
                return rv;

            return Convert.ToInt64(StrToFloat(str, defValue));
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out intValue);
            }
            return intValue;
        }

        /// <summary>
        /// 将对象转换为金额，即两位小数的decimal类型,转换失败返回0.00M,
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal ObjectToCurrency(object expression)
        {
            decimal dec = ObjectToDecimal(expression, 0.00M);
            return Math.Round(dec, 2);
        }
        /// <summary>
        /// 将decimal处理为金额，保留两位小数.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static decimal RoundToCurrency(decimal expression)
        {
            return Math.Round(expression, 2);
        }
        /// <summary>
        /// 将对象转换为decimal类型,转换失败返回0
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal ObjectToDecimal(object expression)
        {
            return ObjectToDecimal(expression, 0);
        }

        /// <summary>
        /// 将对象转换为decimal类型,转换失败返回defValue
        /// </summary>
        /// <param name="expression">要转换的对象</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal ObjectToDecimal(object expression, decimal defValue)
        {
            if (expression != null && !(expression is System.DBNull))
                return StrToDecimal(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将字符串转换为decimal类型,转换失败返回0
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal StrToDecimal(string expression)
        {
            return StrToDecimal(expression, 0);
        }

        /// <summary>
        /// 将字符串转换为decimal类型,转换失败返回defValue
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal StrToDecimal(string expression, decimal defValue)
        {
            if (string.IsNullOrEmpty(expression) ||  !IsNumeric(expression.Trim()))
                return defValue;

            try
            {
                decimal rv;
                if (decimal.TryParse(expression, out rv))
                    return rv;
            }
            catch
            {
                return defValue;
            }
            return defValue;
        }
        
        /// <summary>
        /// 是否数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumeric(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }
        /// <summary>
        /// 是否整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*$");
        }

        /// <summary>
        /// 是否正整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInteger(string value)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[0-9]*[1-9][0-9]*$");
        }

    }
}
